<template>
  <div class="page">

    <!-- 搜索区域 开始 -->
    <div class="el_card_box">
      <el-form :inline="true" size="small" :model="formInline" class="search_form">
        <el-form-item label="输出时间：">
          <el-date-picker v-model="formInline.orderTime" type="daterange" range-separator="至" start-placeholder="开始日期" end-placeholder="结束日期" value-format="yyyy-MM-dd">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="回款时间：">
          <el-date-picker v-model="formInline.orderTime2" type="daterange" range-separator="至" start-placeholder="开始日期" end-placeholder="结束日期" value-format="yyyy-MM-dd">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="结算状态：">
          <el-select v-model="formInline.settleStatus" class="a_select" placeholder="请选择" clearable>
            <el-option v-for="(item, index) in BILLSETTLESTATUS" :key="index" :label="item.name" :value="item.id">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="城市：">
          <city-select v-model="formInline.cities"></city-select>
        </el-form-item>
        <el-form-item label="门诊：">
          <hospital-select v-model="formInline.hospitals"></hospital-select>
        </el-form-item>
        <el-form-item label="对接人：">
          <user-select v-model="formInline.principals"></user-select>
        </el-form-item>
        <el-form-item label="发票需求：">
          <el-select v-model="formInline.invoiceNeed" class="a_select" placeholder="请选择" clearable>
            <el-option v-for="(item, index) in INVOICENEED" :key="index" :label="item.name" :value="item.id">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="发票进度：">
          <el-select v-model="formInline.invoiceProgress" class="a_select" placeholder="请选择" clearable>
            <el-option v-for="(item, index) in INVOICEPROGRESS" :key="index" :label="item.name" :value="item.id">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="实际结算金额：">
          <el-input v-model="formInline.actualSettleAmount" placeholder="请输入"></el-input>
        </el-form-item>
      </el-form>
      <div class="search_btn_box">
        <el-button type="primary" size="small" @click="getList(1)">筛选</el-button>
        <el-button type="default" size="small" @click="rest">清空筛选</el-button>
      </div>
    </div>
    <!-- 搜索区域 end -->

    <!-- 表格 开始 -->
    <div class="table_box">
      <!-- 操作区域 开始 -->
      <div class="action_box">
        <el-button type="primary" size="small" :loading="eloading" @click="downloadExcel()">批量导出</el-button>
      </div>
      <!-- 操作区域 end -->

      <el-table ref="elTable" class="el_table" size="small" show-summary :summary-method="getSummaries" :data="tableData" :loading="loading">
        <el-table-column prop="cityName" label="城市" show-overflow-tooltip />
        <el-table-column prop="hospitalName" label="门诊" show-overflow-tooltip />
        <el-table-column prop="principal" label="对接人" show-overflow-tooltip />
        <el-table-column prop="outputTime" label="输出时间" show-overflow-tooltip />
        <el-table-column prop="oughtSettleAmount" label="应结算额" show-overflow-tooltip />
        <el-table-column prop="actualSettleAmount" label="实际结算额" show-overflow-tooltip />
        <el-table-column prop="collectionTime" label="回款时间" show-overflow-tooltip />
        <el-table-column prop="collectionCycle" label="回款周期" show-overflow-tooltip />
        <el-table-column prop="transferVoucher" label="转款截图" width="240">
          <template slot-scope="{row}">
            <gl-image v-for="(item, index) in showImages(row.transferVoucher)" :key="index" :src="item" class="tab_small_img" alt="" />
          </template>
        </el-table-column>
        <el-table-column prop="invoiceNeed" label="发票需求" show-overflow-tooltip />
        <el-table-column prop="invoiceProgress" label="发票进度" show-overflow-tooltip />
        <el-table-column prop="statusStr" label="结算状态" show-overflow-tooltip />
        <el-table-column prop="remark" label="备注" />
        <el-table-column prop="address" label="操作" width="240" fixed="right">
          <template slot-scope="{row}">
            <el-link type="primary" class="el_link" v-if="row.status == 1" @click="openSettleDialog(row)">查看明细</el-link>
            <el-link type="primary" class="el_link" v-if="row.status != 1" @click="openSettleDialog(row)">账单修改</el-link>
            <el-link type="primary" class="el_link" v-if="row.status != 1" @click="openDialog(row, 1)">结算完成</el-link>
            <el-link type="primary" class="el_link" v-if="row.status == 1" @click="openDialog(row, 2)">信息补充</el-link>
            <el-link type="primary" class="el_link" @click="signleExport(row)">导出</el-link>
            <el-link type="danger" class="el_link" @click="signleRemove(row)">删除</el-link>
          </template>
        </el-table-column>
      </el-table>
      <div class="pagination_list">
        <el-pagination background layout="sizes, total,prev, pager, next, jumper" :total="pageTotal" :page-size="pageSize" :page-sizes="pageSizes" :current-page.sync="pageNumber" @current-change="getList" @size-change="changePageSize" />
      </div>
    </div>
    <!-- 表格 end -->

    <!-- 弹框 开始 -->
    <glsx-dialog :show="showDialog" :title="dialogTitle" @close="closeDialog()">
      <info-ment ref="settleComponent" v-if="showDialog" :info="settleInfo" :id="settleId" @change="onChange"></info-ment>
      <div slot="footer">
        <el-button type="primary" size="small" :loading="aloading" @click="saveInfo('settleComponent')">保存</el-button>
        <el-button type="default" size="small" @click="closeDialog()">取消</el-button>
      </div>
    </glsx-dialog>
    <!-- 弹框 end -->

  </div>
</template>

<script>
import {
  BASEURL,
  GET_BILLINGRECORDS_SEARCH,
  GET_BILLINGRECORDS_EXPORT,
  GET_BILLINGRECORDS_SINGLEEXPORT,
  DELETE_BILLINGRECORDS_DETAIL
} from "@/http/finance";
import { showModal, showToast, deepClone, broadcast, received } from "@/common/util";
import { exportExcel } from "@/common/common";
import { INVOICENEED, INVOICEPROGRESS, BILLSETTLESTATUS } from "@/common/constants";
import InfoMent from "@/components/common/info";
export default {
  components: {
    InfoMent,
  },
  data() {
    return {
      formInline: {},
      tableData: [],
      pageTotal: 0,
      pageNumber: 1,
      pageSizes: [10, 20, 50, 100],
      pageSize: 20,
      loading: false,
      eloading: false,
      INVOICENEED,
      INVOICEPROGRESS,
      BILLSETTLESTATUS,

      // 弹框
      showDialog: false,
      dialogTitle: "信息补充",
      settleInfo: {},
      settleId: '',
      aloading: false,
      //合计
      summary: {}
    };
  },
  mounted() {
    this.getList();
    let _this = this;
    received("customer.add.success", function () {
      _this.getList();
    });

  },
  methods: {
    /** 获取用户列表 */
    getList(current) {
      let _this = this;
      let oParams = _this.getParams();
      oParams.pageNumber = current || _this.pageNumber;
      console.log(oParams);
      _this.loading = true;
      GET_BILLINGRECORDS_SEARCH(oParams).then((res) => {
        console.log(res);
        _this.loading = false;
        let obj = res.data || {};
        _this.tableData = obj.list || [];
        _this.pageTotal = obj.totalElements;
        _this.summary = res.summary || {}
      });
    },
    /** 每页展示 */
    changePageSize(e) {
      this.pageSize = e;
      this.getList();
    },

    /** 获取参数 */
    getParams() {
      let o = JSON.parse(JSON.stringify(this.formInline));
      let m = o.orderTime || []
      let n = o.orderTime2 || []
      if (m && m.length) {
        o.outputStartTime = m[0]
        o.outputEndTime = m[1]
      }
      if (n && n.length) {
        o.collectionStartTime = n[0]
        o.collectionEndTime = n[1]
      }

      delete o.orderTime
      delete o.orderTime2

      o.pageSize = this.pageSize;
      return o;
    },

    /** 重置 */
    rest() {
      this.formInline = {};
      this.pageNumber = 1;
      this.getList();
    },

    // 导出excel
    downloadExcel() {
      let _this = this;
      let o = deepClone(_this.formInline);
      let m = Object.keys(o);
      if (m.length > 0) {
        showModal({
          message: "确定是否导出筛选条件下面所有账单记录列表?",
          showCancelButton: true,
          type: "warning",
          success: function () {
            _this.dowloadExcelFile();
          },
        });
        return false;
      }

      _this.dowloadExcelFile();
    },

    // 下载excel
    dowloadExcelFile() {
      let _this = this;
      let oParams = _this.getParams();
      let options = {};

      delete oParams.pageNumber;
      delete oParams.pageSize;

      options.method = "GET";
      options.data = oParams;
      options.url = GET_BILLINGRECORDS_EXPORT;
      options.fileName = "账单记录列表.xlsx";
      _this.eloading = true;
      exportExcel(options).then((res) => {
        _this.eloading = false;
      });
    },

    //单挑记录导出
    signleExport(row) {
      let _this = this;
      let oParams = { id: row.id };
      let options = {};

      delete oParams.pageNumber;
      delete oParams.pageSize;

      options.method = "GET";
      options.data = oParams;
      options.url = GET_BILLINGRECORDS_SINGLEEXPORT;
      options.fileName = "输出结算列表.xlsx";
      exportExcel(options)
    },

    // 打开输出结算弹框
    openSettleDialog(o) {
      //let o = this.formInline;
      let options = {};
      options.city = o.city;
      options.hospital = o.hospitalId;
      options.settleStatus = o.status;
      options.id = o.id
      options.type = 'records'
      options.outputTime = o.outputTime
      broadcast("settle.dialog.open", options);
    },

    // 打开弹框
    openDialog(row, type) {
      let titles = {
        1: '结算完成',
        2: '信息补充'
      }
      this.showDialog = true;
      this.dialogTitle = titles[type];
      this.settleInfo = row;
      if (type == 2) {
        this.settleId = row.id
      }
    },

    // 关闭弹框
    closeDialog() {
      this.showDialog = false;
      this.settleId = ''
      this.settleInfo = {}
    },

    // 保存数据
    saveInfo() {
      this.$refs.settleComponent.saveInfo();
      this.aloading = true;
    },

    // 监听组件变化
    onChange(e) {
      this.aloading = false;
      if (e) {
        this.closeDialog();
        this.getList();
      }
    },

    // 展示图片
    showImages(str) {
      let list = [];
      if (!str) {
        return list;
      }
      let arr = str.split(",");

      arr.map((k) => {
        let o = BASEURL + k;
        list.push(o);
      });
      return list;
    },

    // 获取统计数据
    getSummaries(param) {
      const { columns, data } = param;
      const sums = [];
      let list = ['oughtSettleAmount', 'actualSettleAmount'];
      let o = this.summary
      columns.forEach((column, index) => {
        if (index === 0) {
          sums[index] = '合计';
          return;
        }
        const values = data.map(item => Number(item[column.property]));
        let idx = list.indexOf(column.property)
        if (idx >= 0) {
          sums[index] = o[column.property];
        } else {
          sums[index] = '';
        }
      });
      return sums;
    },

    // 删除
    signleRemove(row) {
      let _this = this;
      showModal({
        message: "确定删除该记录吗？",
        showCancelButton: true,
        success: function () {
          DELETE_BILLINGRECORDS_DETAIL({ id: row.id }).then((res) => {
            if (res.code == 200) {
              showToast({
                message: "删除成功",
                type: "success",
              });
              _this.getList();
            }
          });
        },
      });
    }

  },
};
</script>

<style lang="scss" scoped>
.tab_small_img {
  width: 40px;
  height: 40px;
  margin: 4px;
  border-radius: 4px;
}
</style>
